from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException, Request, Form
from sqlalchemy.orm import Session

from app.utils.resp_util import APIResponse
from app.extensions import dependencies as deps
from app.extensions.dependencies import get_query_token2, parse_encryption_header
from app.dependencies.db import get_db
from app.common import exceptions as excs
from app.common.enums.resp_enum import ErrTypes

#from app.models.trans import PayOrder
from app.schemas.merchant import MerchantDetail
from app.schemas.trans import PayOrder, NewPayOrder
from app.services import merchant as merc_svc
from app.services import trans

import logging

logger = logging.getLogger(__name__)

router = APIRouter(prefix='/trans', tags=['transaction'])
#router = APIRouter(dependencies=[Depends(get_query_token2)],)

@router.post("/initPayOrder", response_model=APIResponse)
def pay_order(params: Annotated[dict, Depends(parse_encryption_header)], db: Session = Depends(get_db)):
    logger.info('>> initPayOrder')
    # todo
    return 1

@router.post("/initDaifuOrder", response_model=APIResponse[PayOrder])
async def daifuOrder(params: Annotated[dict, Depends(parse_encryption_header)], db: Session = Depends(get_db)):
    logger.debug('>> initDaifuOrder')
    try:
        logger.debug('params: %s' % params)
        resp = APIResponse()
        resp.data = trans.insert_daifu_order(db=db, params=params)
        return resp
    except excs.GenericException as err:
        logger.error(err)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.error(err)
        raise excs.GenericException(ErrTypes.bz_error)
